﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Engine.Graphics
{
    public struct ParticleSystemData
    {
        public Texture2D ParticleTexture;

        public float MaxLifetime;
        public int ParticleSpawnRate;

        public SpawnArea SpawnAreaType;

        public Vector2 StartDirection;
        public float StartSpeed;
        public Vector2 StartScale;
        public float SpawnRadius;
        public float LifetimeRandom;
        public float VelocityRandom;

        public Color[] animationColors;

        public Vector2 EndScale;
        public float EndRotation;

        public Animation animation;

        public ParticleSystemData(Texture2D _texture, Animation _anim, float _maxLifetime, int _particleSpawnRate,
                                  Vector2 _startScale, Vector2 _startDirection, float _startSpeed,
                                  Color _Color01, Color _Color02, Color _Color03, Color _Color04, Color _Color05, 
                                  float _spawnRadius, float _lifetimeRandom, float _velocityRandom, 
                                  Vector2 _endScale, float _endRotation,
                                  SpawnArea _spawnAreaType)
        {
            ParticleTexture = _texture;
            animation = _anim;

            MaxLifetime = _maxLifetime;
            ParticleSpawnRate = _particleSpawnRate;
            StartScale = _startScale;
            _startDirection.Normalize();
            StartDirection = _startDirection;
            StartSpeed = _startSpeed;
            SpawnRadius = _spawnRadius;
            LifetimeRandom = _lifetimeRandom;
            VelocityRandom = _velocityRandom;
            EndScale = _endScale;
            EndRotation = _endRotation;
            SpawnAreaType = _spawnAreaType;
            animationColors = new Color[5];
            animationColors[0] = _Color01;
            animationColors[1] = _Color02;
            animationColors[2] = _Color03;
            animationColors[3] = _Color04;
            animationColors[4] = _Color05;
        }
    }
    
    static class ParticleSystemDatabase
    {
        static public Dictionary<string, ParticleSystemData> ParticleSystemPresets;

        static ParticleSystemDatabase()
        {
            ParticleSystemPresets = new Dictionary<string, ParticleSystemData>();
            ParticleSystemPresets["Fire"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\fire"),
                new Animation(1, 150, 150, 0,0, true),
                1.5f,
                1600,
                new Vector2(0.3f),
                new Vector2(0, -1),
                60,
                Color.FromNonPremultiplied(173, 216, 230, 40),
                Color.FromNonPremultiplied(190, 190, 130, 100),
                Color.FromNonPremultiplied(200, 140, 0, 120),
                Color.FromNonPremultiplied(255, 0, 0, 100),
                Color.FromNonPremultiplied(255,0,0,20),
                3f, 0.1f, 0.2f,
                new Vector2(.1f),
                3,
                SpawnArea.Circle);

            ParticleSystemPresets["FireWall"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\fire"),
                new Animation(1, 50, 50, 0, 0, true),
                .8f,
                2000,
                new Vector2(0.9f),
                new Vector2(0, -1),
                150,
                Color.FromNonPremultiplied(238, 78, 40, 200),
                Color.FromNonPremultiplied(253, 160, 60, 200),
                Color.FromNonPremultiplied(252, 140, 60, 200),
                Color.FromNonPremultiplied(255, 112, 40, 200),
                Color.FromNonPremultiplied(200, 200, 200, 40),
                23f, 0.1f, 0.2f,
                new Vector2(.3f),
                3,
                SpawnArea.Line);

            ParticleSystemPresets["TestSystem"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\fire"),
                new Animation(1, 50, 50, 0, 0, true),
                .8f,
                2900,
                new Vector2(0.9f),
                new Vector2(0, -1),
                150,
                Color.FromNonPremultiplied(238, 78, 40, 200),
                Color.FromNonPremultiplied(253, 160, 60, 200),
                Color.FromNonPremultiplied(252, 140, 60, 200),
                Color.FromNonPremultiplied(255, 112, 40, 200),
                Color.FromNonPremultiplied(200, 200, 200, 40),
                13f, 0.1f, 0.2f,
                new Vector2(.3f),
                3,
                SpawnArea.Line);

            ParticleSystemPresets["FlameWisp"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\FlameWisp"),
                new Animation(14, 66, 62, 0, 0, true),
                .8f,
                1000,
                new Vector2(1f),
                new Vector2(0, -1),
                120,
                Color.White,
                Color.White,
                Color.White,
                Color.White,
                Color.White,
                15f, 0.01f, 0.1f,
                new Vector2(1f),
                0,
                SpawnArea.Circle);

            ParticleSystemPresets["FlameWisp2"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\FireBall"),
                new Animation(5, 62, 61, 0, 0, true),
                0.9f,
                800,
                new Vector2(3f),
                new Vector2(0, -1),
                80,
                Color.White,
                Color.White,
                Color.White,
                Color.White,
                Color.White,
                4f, 0.01f, 0.1f,
                new Vector2(3f),
                0,
                SpawnArea.Circle);

            ParticleSystemPresets["FireArrow"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\fire"),
                new Animation(1, 50, 50, 0, 0, true),
                .15f,
                8000,
                new Vector2(0.4f),
                new Vector2(0, -1),
                60,
                Color.FromNonPremultiplied(238, 160, 40, 255),
                Color.FromNonPremultiplied(253, 160, 60, 255),
                Color.FromNonPremultiplied(252, 140, 60, 255),
                Color.FromNonPremultiplied(255, 112, 40, 255),
                Color.FromNonPremultiplied(200, 200, 200, 40),
                1f, 0.1f, 0.2f,
                new Vector2(.1f),
                3,
                SpawnArea.Circle);

            ParticleSystemPresets["ExplosionArrow"] = new ParticleSystemData(
                GraphicsEngine.Instance.GetTexture(@"ParticleTextures\fire"),
                new Animation(1, 50, 50, 0, 0, true),
                .75f,
                20,
                new Vector2(1f),
                new Vector2(0, -1),
                50,
                Color.FromNonPremultiplied(173, 216, 230, 30),
                Color.FromNonPremultiplied(173, 216, 130, 150),
                Color.FromNonPremultiplied(200, 150, 0, 180),
                Color.FromNonPremultiplied(255, 0, 0, 50),
                Color.FromNonPremultiplied(200, 0, 0, 10),
                4f, 0.1f, 0.2f,
                new Vector2(.3f),
                4,
                SpawnArea.Circle);
        }
    }
}
